package com.retailshop.shopuser.WapUserController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.retailshop.shopuser.common.*;
import com.retailshop.shopuser.entity.*;
import com.retailshop.shopuser.service.UserMessagesService;
import com.retailshop.shopuser.service.UserNoService;
import com.retailshop.shopuser.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.List;

@CrossOrigin
@RequestMapping("/wap")
@Controller
public class WapUserCenterController {

    @Autowired
    UserNoService userNoService;
    @Autowired
    UserService userService;

    @Autowired
    UserMessagesService userMessagesService;

    @Autowired
    TokenUtils tokenUtils;
    /**
     * 修改支付密码
     *
     * @param
     * @param request
     * @return
     */
    @UserLoginToken
    @ResponseBody
    @RequestMapping(value = "/updatePayPwd", method = RequestMethod.POST)
    public Response updatePayPwd(HttpServletRequest request) {

        boolean flag = false;
        try {
            flag = userNoService.updatePayPwd(request);
            if (flag) {
                return new Response(MessageConstants.MSG_SUCCESS, flag, 0, "", request);
            } else {
                return new Response(MessageConstants.MSG_VERIFY_ERROR, flag, 0, "", request);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return new Response(MessageConstants.MSG_SYS_ERROR, flag, 0, "", request);
        }

    }

    /**
     * 获取分享人
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getSharers")
    public Response getSharers(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        int userNoId = 0;
        if (userNoIds != null && userNoIds != "") {
            if (StringUtil.isNumeric(userNoIds)) {
                userNoId = Integer.parseInt(userNoIds);
            } else {
                return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
            }

        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }
        List<UserSharerInfo> userSharerInfoList = userNoService.getUserSharesInfo(userNoId);

        return new Response(MessageConstants.MSG_SUCCESS, userSharerInfoList, 0, "", request);

    }

    /**
     * 查询用户手机号与编号
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getSimpleUserInfo")
    public Response getSimpleUserInfo(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        int userNoId = 0;
        if (userNoIds != null && userNoIds != "") {
            userNoId = Integer.parseInt(userNoIds);
        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        SimpleUserInfo simpleUserInfo = userNoService.getSimpleUserInfo(userNoId);
        if (simpleUserInfo != null) {
            return new Response(MessageConstants.MSG_SUCCESS, simpleUserInfo, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_ITEM_NOT_EXIST, null, 0, "", request);
        }

    }

    /**
     * 查询手机号绑定的账号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserNoList")
    public Response getUserNoList(HttpServletRequest request) {
        String userNoIds = request.getParameter("userNoId");
        int userNoId = 0;
        int userId = 0;
        List<UserNoLoginInfo> userNoLoginInfoList = null;

        if (userNoIds != null) {
            if (StringUtil.isNumeric(userNoIds)) {
                userNoId = Integer.parseInt(userNoIds);
            }
            userNoLoginInfoList = userNoService.getUserNoList(userNoId);

            if (userNoLoginInfoList != null && userNoLoginInfoList.size() != 0) {

                return new Response(MessageConstants.MSG_SUCCESS, userNoLoginInfoList, 0, "", request);
            } else {
                return new Response(MessageConstants.NO_USERNO, null, 0, "", request);
            }


        } else {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }


    }

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserMessages")
    public Response getUserMessages(HttpServletRequest request) {

        String userNoIds = request.getParameter("userNoId");
        String pages = request.getParameter("page");
        int page = 0;
        int limit = 5;

        int userNoId = 0;
        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        }

        if (pages == null || pages == "" || !StringUtil.isNumeric(pages)) {
            page = Integer.parseInt(pages);
        }

        PageHelper.startPage(page, limit);
        List<UserMessages> userMessagesList = userMessagesService.getMessages(userNoId);
        PageInfo<UserMessages> pageInfo = new PageInfo<>();
        return new Response(MessageConstants.MSG_SUCCESS, pageInfo.getList(), pageInfo.getTotal(), "", request);
    }

    /**
     * 增加用户消息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/addUserMessage")
    public Response addUserMessage(HttpServletRequest request) {

        String userNoIds = request.getParameter("userNoId");
        String messageContent = request.getParameter("messageContent");

        int userNoId = 0;
        if (userNoIds == null || userNoIds == "" || !StringUtil.isNumeric(userNoIds)) {
            userNoId = Integer.parseInt(userNoIds);
        }
        if (messageContent == null || messageContent == "") {
            return new Response(MessageConstants.MESSAGES_NULL, null, 0, "", request);
        }

        UserMessages userMessages = new UserMessages();
        userMessages.setCreateTime(new Timestamp(System.currentTimeMillis()));
        userMessages.setMessageContent(messageContent);
        userMessages.setMessageState("1");
        userMessages.setUserNoId(userNoId);
        int row = userMessagesService.insertSelective(userMessages);
        if (row > 0) {
            return new Response(MessageConstants.MSG_SUCCESS, row, 0, "", request);
        } else {
            return new Response(MessageConstants.MSG_SYS_ERROR, null, 0, "", request);
        }

    }

    /**
     * 切换账号
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/switchoverUserNo",method = RequestMethod.POST)
    public Response switchoverUserNo(HttpServletRequest request, HttpServletResponse response) {
        String userNos= request.getParameter("userNo");
        String loginSign= request.getParameter("loginSign");
        JSONObject jsonObject = new JSONObject();
        UserNo userNo=userNoService.getUserNoByUserNo(userNos);//用户账号查询
        if(userNo==null){
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        if ("1".equals(loginSign)) {//手机号登录不需要验证码:1为手机号登录，0为账号登录
            String token = tokenUtils.getToken(userNo);
            jsonObject.put("token", token);
            jsonObject.put("userNo", userNo);
            return new Response(MessageConstants.MSG_SUCCESS, jsonObject, 0, "", request);

        }else{//账号登录
            String phone = request.getParameter("phone");
            String code = request.getParameter("code");
            String codeTime = request.getParameter("codeTime");
            String requestHash = request.getParameter("hash");
            boolean flag = CheckCode.checkCode(phone, code, codeTime, requestHash);
            if(flag){
                String token = tokenUtils.getToken(userNo);
                jsonObject.put("token", token);
                jsonObject.put("userNo", userNo);
                return new Response(MessageConstants.MSG_SUCCESS, jsonObject, 0, "", request);
            }else{
                return new Response(MessageConstants.MSG_VERIFY_ERROR, jsonObject, 0, "", request);
            }
        }

    }

    /**
     * 获取手机号下的所有的账号
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("/getUserNos")
    public Response getUserNosByPhone(HttpServletRequest request) {
        String userIds = request.getParameter("userId");
        String userNoIds = request.getParameter("userNoId");
        if (userIds == null || userIds == "" || !StringUtil.isNumeric(userIds)||userNoIds==null||userNoIds==""||!StringUtil.isNumeric(userNoIds)) {
            return new Response(MessageConstants.MSG_ILLEGAL_ARGUMENT, null, 0, "", request);
        }

        int userId = 0;
        int userNoId=0;
        userId = Integer.parseInt(userIds);
        userNoId = Integer.parseInt(userNoIds);

       List<SwitchUserNo> switchUserNos= userNoService.getUserNoListByUserId(userId,userNoId);

        return new Response(MessageConstants.MSG_SUCCESS, switchUserNos, 0, "", request);

    }

    @ResponseBody
    @RequestMapping("/queryUserNo")
    public Response queryUserNo(HttpServletRequest request){
        String no=request.getParameter("userNo");
        UserNo userNo=null;
        if(no==null||no==""){
            return new Response(MessageConstants.MSG_SUCCESS,"0", 0, "", request);
        }
        userNo=userNoService.getUserNoByUserNo(no);
        if(userNo!=null){
            return  new Response(MessageConstants.MSG_SUCCESS, "1", 0, "", request);
        }else{
            return  new Response(MessageConstants.MSG_SUCCESS, "0", 0, "", request);
        }

    }

}
